package com.fay.mymiaosha.service.impl;

import com.fay.mymiaosha.dao.ItemDoMapper;
import com.fay.mymiaosha.dao.ItemStockDoMapper;
import com.fay.mymiaosha.dataobject.ItemDo;
import com.fay.mymiaosha.dataobject.ItemStockDo;
import com.fay.mymiaosha.exception.BizException;
import com.fay.mymiaosha.response.EmBizError;
import com.fay.mymiaosha.service.IItemService;
import com.fay.mymiaosha.service.IPromoService;
import com.fay.mymiaosha.service.model.ItemModel;
import com.fay.mymiaosha.service.model.PromoModel;
import com.fay.mymiaosha.validator.ValidationResult;
import com.fay.mymiaosha.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/3/17 20:48
 * @Version 1.0
 */
@Service
public class ItemServiceImpl implements IItemService {

    @Autowired
    private ItemDoMapper itemDoMapper;

    @Autowired
    private ItemStockDoMapper itemStockDoMapper;

    @Autowired
    private ValidatorImpl validator;

    @Autowired
    private IPromoService promoService;

    @Override
    public List<ItemModel> listItems() {
        List<ItemDo> itemDoList = itemDoMapper.listItem();
        // convert
        List<ItemModel> itemModels = itemDoList.stream().map(itemDo -> {
            ItemStockDo itemStockDo = itemStockDoMapper.selectByItemId(itemDo.getId());
            return convertFromItemDo(itemDo, itemStockDo);
        }).collect(Collectors.toList());
        return itemModels;
    }

    @Override
    public ItemModel getItemById(Integer id) {
        ItemDo itemDo = itemDoMapper.selectByPrimaryKey(id);
        // 查完数据先检查是否为空
        if (itemDo == null) {
            return null;
        }
        // 聚合 stock信息
        ItemStockDo itemStockDo = itemStockDoMapper.selectByItemId(itemDo.getId());
        ItemModel itemModel = convertFromItemDo(itemDo, itemStockDo);

        // 聚合 活动信息
        PromoModel promoModel = promoService.getPromoByItemId(itemDo.getId());

        // 如果存在该商品秒杀对象并且秒杀状态不等于3，则说明有效
        if (!Objects.isNull(promoModel) && promoModel.getStatus() != 3) {
            itemModel.setPromoModel(promoModel);
        }
        return itemModel;
    }

    @Override
    @Transactional
    public ItemModel createItem(ItemModel itemModel) throws BizException {
        // 在srv 进行参数校验
        ValidationResult validationResult = validator.validate(itemModel);
        if (validationResult.isHasError()) {
            throw new BizException(EmBizError.PARAMETER_VALIDATION_ERROR, validationResult.getErrMsg());
        }
        // convert to entity
        ItemDo itemDo = convertItemDoFromItemModel(itemModel);
        // insert selective
        itemDoMapper.insertSelective(itemDo);
        // convert to entity
        ItemStockDo itemStockDo = convertItemStockDoFromItemModel(itemModel);
        // after creating then return new instance
        return this.getItemById(itemModel.getId());
    }

    @Override
    public void increaseSales(Integer itemId, Integer amount) throws BizException {
        itemDoMapper.addSalesByItemId(itemId, amount);
    }

    @Override
    public boolean decreaseStock(Integer itemId, Integer amount) {
        int affectRow = itemStockDoMapper.decreaseStock(itemId, amount);
        return affectRow > 0;
    }

    // convert model to entity1
    private ItemStockDo convertItemStockDoFromItemModel(ItemModel itemModel) {
        if (itemModel == null) {
            return null;
        }
        ItemStockDo itemStockDo = new ItemStockDo();
        itemStockDo.setStock(itemModel.getStock());
        itemStockDo.setItemId(itemModel.getId());
        return itemStockDo;
    }

    // convert model to entity2
    private ItemDo convertItemDoFromItemModel(ItemModel itemModel) {
        if (itemModel == null) {
            return null;
        }
        ItemDo itemDo = new ItemDo();
        BeanUtils.copyProperties(itemModel, itemDo);
        itemDo.setPrice(itemModel.getPrice().doubleValue());
        return itemDo;
    }

    // convert entity to model
    private ItemModel convertFromItemDo(ItemDo itemDo, ItemStockDo itemStockDo) {
        if (itemDo == null) {
            return null;
        }
        ItemModel itemModel = new ItemModel();
        BeanUtils.copyProperties(itemDo, itemModel);
        itemModel.setPrice(BigDecimal.valueOf(itemDo.getPrice()));
        itemModel.setStock(itemStockDo.getStock());
        return itemModel;
    }
}
